package com.meichis.yndms.hybrid;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.webkit.JavascriptInterface;
import android.widget.DatePicker;
import android.widget.Toast;

import com.meichis.mcsappframework.utils.MessageTools;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class JavaScriptInterface {
	private Context mContext;
	public final static int CONTEXT_RETURNTAKEPHOTO = 1;
	public final static int CONTEXT_RETURNGETFILE = 2;
	public final static int CONTEXT_RETURNLOCALLOGIN = 100;
	public final static int CONTEXT_RETURNWEBLOGIN = 200;
	public final static int CONTEXT_RETURNSCANCODE = 500;
	public final static int CONTEXT_RETURNLOCATION = 300;
	/** Instantiate the interface and set the context */
	private HybridInterface hybrid;
	private MCWebView webView;

	public JavaScriptInterface(Context c, MCWebView webView, HybridInterface _callback) {
		mContext = c;
		hybrid = _callback;
		this.webView = webView;
	}
	@JavascriptInterface
	public void JsCall(final String CallType, String toast, final String ControlID, final String FUNCCallBack) {
		try {
			if (CallType.equals("POS")) {
				hybrid.execute(-400, ControlID, toast, FUNCCallBack);
			}
			if (CallType.equals("Print")) {
				hybrid.execute(-500, ControlID, toast, FUNCCallBack);
			}
			if (CallType.equals("CustomPrint")) {
				hybrid.execute(-600, ControlID, toast, FUNCCallBack);
			}
			if (CallType.equalsIgnoreCase("READNFC")) {
				hybrid.execute(-700, ControlID, toast, FUNCCallBack);
			}
			if (CallType.equals("SCAN")) { // 扫码(1)
				BarcodeScannerPlugin plugin = new BarcodeScannerPlugin(hybrid, webView);
				plugin.execute(new CallbackContext(FUNCCallBack, ControlID, webView));
			} else if (CallType.equals("PHOTO")) {// 拍照(2)
				// uicall.execute(2,ControlID, "", FUNCCallBack);
			} else if (CallType.equals("GENGRAPHY")) {// 地理位置(3)
				// uicall.execute(3,ControlID, "", FUNCCallBack);
			}  else if (CallType.toUpperCase(Locale.getDefault()).equals("CLOSE")) {
				hybrid.getActivity().finish();
			} else if (CallType.equalsIgnoreCase("SHOWPROGRESS")) {// 弹出加载框
				try {
					hybrid.getActivity().showDialog(2);
				} catch (Exception e) {
					// TODO: handle exception
				}
			} else if (CallType.equalsIgnoreCase("REMOVEPROGRESS")) {// 消除加载框
				try {
					hybrid.getActivity().removeDialog(2);
				} catch (Exception e) {
					// TODO: handle exception
				}
			} else if (CallType.equals("SHOWCONFIRMMSG")) {// 确认消息弹屏(6)
				ShowConfirmMSG(6, toast, ControlID, FUNCCallBack);
			}  else if (CallType.equals("SETMIDDLETITLE")) {// 设置中间标题
				hybrid.execute(-200, ControlID, toast, "");
			} else if (CallType.equals("SETRIGHTTITLE")) {// 设置右上角按钮
				hybrid.execute(-300, ControlID, toast, "");
			} else if (CallType.equals("GETDATE")) {// 选择日期
				final Calendar cd = Calendar.getInstance();
				Date date = new Date(System.currentTimeMillis());
				cd.setTime(date);
				new DatePickerDialog(hybrid.getActivity(), new OnDateSetListener() {
					@Override
					public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
						String datestr = year + "-" + (String.valueOf(monthOfYear + 1).length() == 1 ? "0" + String.valueOf(monthOfYear + 1) : String.valueOf(monthOfYear + 1)) + "-"
								+ (String.valueOf(dayOfMonth).length() == 1 ? "0" + String.valueOf(dayOfMonth) : String.valueOf(dayOfMonth));
						hybrid.execute(10, ControlID, datestr, FUNCCallBack);
					}
				}, cd.get(Calendar.YEAR), cd.get(Calendar.MONTH), cd.get(Calendar.DAY_OF_MONTH)).show();
			}

		} catch (Exception e) {
		}
	}

	/** Show a toast from the web page */
	public void showToast(int CallType, String toast, final String ControlID, final String FUNCCallBack) {
		Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
		hybrid.execute(CallType, ControlID, "0", FUNCCallBack);
	}

	/**
	 * 弹出对话框
	 */
	public void ShowConfirmMSG(final int CallType, String MSG, final String ControlID, final String FUNCCallBack) {

		new AlertDialog.Builder(mContext).setMessage(MSG).setNeutralButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				hybrid.execute(CallType, ControlID, "true", FUNCCallBack);
			}
		}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int arg1) {
				hybrid.execute(CallType, ControlID, "false", FUNCCallBack);
			}
		}).show();
	}

	/**
	 * 弹出消息框
	 */
	public void ShowMSG(int CallType, String MSG, final String ControlID, final String FUNCCallBack) {
		MessageTools.showLongToast(  MSG);
		hybrid.execute(CallType, ControlID, "0", FUNCCallBack);
	}
}
